package com.surfacing.rcp.model.codec;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;


import org.apache.log4j.Logger;

import com.surfacing.rcp.model.common.AbstractMessage;
import com.surfacing.rcp.model.entity.Anforderung;
import com.surfacing.rcp.model.entity.Dienststelle;
import com.surfacing.rcp.model.entity.Doctor;
import com.surfacing.rcp.model.entity.Einsatzart;
import com.surfacing.rcp.model.entity.Journal;
import com.surfacing.rcp.model.entity.Naca;
import com.surfacing.rcp.model.entity.Nfs;
import com.surfacing.rcp.model.entity.Transportziel;
import com.surfacing.rcp.model.factory.ProtocolCodecFactory;

public class JournalDecoder implements MessageDecoder {
	private static final Logger log = Logger.getLogger(JournalDecoder.class);

	@Override
	public AbstractMessage doDecode(XMLEventReader reader)
			throws XMLStreamException {

		// Create a new staff member
		Journal member = new Journal();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		Timestamp ts;

		// parse and set up the object
		while (reader.hasNext()) {
			// the type of the event
			XMLEvent event = reader.nextEvent();
			if (event.isStartElement()) {
				String startName = event.asStartElement().getName()
						.getLocalPart();
				// create a new item
				if (Journal.ID.equalsIgnoreCase(startName))
					member = new Journal();

				// get the type of the element and set the corresponding value
				if ("journalId".equalsIgnoreCase(startName))
					member.setId(Integer.parseInt(reader.getElementText()));
				if ("protokollnummer".equalsIgnoreCase(startName))
					member.setProtokollnummer(reader.getElementText());
				// get the type of the element and set the corresponding value
				if ("datum".equalsIgnoreCase(startName)) {
					try {
						date = new Date(formatter
								.parse(reader.getElementText()).getTime());
						member.setDatum(date);
					} catch (ParseException e) {
						log.error("Error on date convertion.", e);
					}
				}
				if ("name".equalsIgnoreCase(startName))
					member.setName(reader.getElementText());
				if (Transportziel.ID.equalsIgnoreCase(startName)) {
					MessageDecoder decoder = ProtocolCodecFactory.getDefault()
							.getDecoder(Transportziel.ID);
					member.setTransportziel((Transportziel) decoder
							.doDecode(reader));
				}
				if ("alarmzeit".equalsIgnoreCase(startName)) {
					formatter = new SimpleDateFormat("HH:mm:ss");
					member.setAlarmZeit(Long.parseLong(reader.getElementText()));
				}
				if ("endzeit".equalsIgnoreCase(startName)) {
					formatter = new SimpleDateFormat("HH:mm:ss");
					member.setEndZeit(Long.parseLong(reader.getElementText()));
				}
				if ("created".equalsIgnoreCase(startName)) {
					formatter = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
					try {
						ts = new Timestamp(formatter.parse(
								reader.getElementText()).getTime());
						member.setCreated(ts);
					} catch (ParseException e) {
						log.error("Error on date convertion.", e);
					}
				}
				if ("changed".equalsIgnoreCase(startName)) {
					formatter = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
					try {
						ts = new Timestamp(formatter.parse(
								reader.getElementText()).getTime());
						member.setChanged(ts);
					} catch (ParseException e) {
						log.error("Error on date convertion.", e);
					}
				}
				if ("notfallort".equalsIgnoreCase(startName))
					member.setNotfallort(reader.getElementText());
				if ("km".equalsIgnoreCase(startName))
					member.setKm(reader.getElementText());
				if (Anforderung.ID.equalsIgnoreCase(startName)) {
					MessageDecoder decoder = ProtocolCodecFactory.getDefault()
							.getDecoder(Anforderung.ID);
					member.setNachforderung((Anforderung) decoder
							.doDecode(reader));
				}
				if (Einsatzart.ID.equalsIgnoreCase(startName)) {
					MessageDecoder decoder = ProtocolCodecFactory.getDefault()
							.getDecoder(Einsatzart.ID);
					member.setEinsatzart((Einsatzart) decoder.doDecode(reader));
				}
				if (Dienststelle.ID.equalsIgnoreCase(startName)) {
					MessageDecoder decoder = ProtocolCodecFactory.getDefault()
							.getDecoder(Dienststelle.ID);
					member.setDienststelle((Dienststelle) decoder
							.doDecode(reader));
				}
				if ("azubi".equalsIgnoreCase(startName))
					member.setAzubi(reader.getElementText());
				if (Nfs.ID.equalsIgnoreCase(startName)) {
					MessageDecoder decoder = ProtocolCodecFactory.getDefault()
							.getDecoder(Nfs.ID);
					member.setNfs((Nfs) decoder.doDecode(reader));
				}
				if (Doctor.ID.equalsIgnoreCase(startName)) {
					MessageDecoder decoder = ProtocolCodecFactory.getDefault()
							.getDecoder(Doctor.ID);
					member.setDoctor((Doctor) decoder.doDecode(reader));
				}
				if (Naca.ID.equalsIgnoreCase(startName)) {
					MessageDecoder decoder = ProtocolCodecFactory.getDefault()
							.getDecoder(Naca.ID);
					member.setNaca((Naca) decoder.doDecode(reader));
				}
				// decode the list of phones
			}
			// check for the end element, and return the object
			if (event.isEndElement()) {
				// get the name
				String endElement = event.asEndElement().getName()
						.getLocalPart();
				// check if we have reached the end
				if (Journal.ID.equalsIgnoreCase(endElement))
					return member;
			}
		}
		return null;
	}
}
